package com.example.mydy.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.UserInfo;
import com.example.mydy.model.UserRewardPrivilege;
import com.example.mydy.utils.MockDataManager;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;

/**
 * 特权用户适配器
 * @deprecated 此适配器已过时，由UserRewardPrivilegeAdapter替代
 */
@Deprecated
public class PrivilegeUserAdapter extends RecyclerView.Adapter<PrivilegeUserAdapter.ViewHolder> {
    private static final String TAG = "PrivilegeUserAdapter";
    
    private Context context;
    private List<UserRewardPrivilege> privileges;
    private List<UserInfo> users;
    private OnPrivilegeEditListener editListener;
    private Consumer<UserInfo> removeUserCallback;
    private boolean taskRewardEnabled = true;
    private boolean inviteRewardEnabled = true;

    /**
     * 构造函数 - 使用UserRewardPrivilege列表
     */
    public PrivilegeUserAdapter(Context context, List<UserRewardPrivilege> privileges) {
        this.context = context;
        this.privileges = privileges;
        this.users = null;
    }
    
    /**
     * 构造函数 - 使用UserInfo列表和删除回调
     */
    public PrivilegeUserAdapter(List<UserInfo> users, Consumer<UserInfo> removeUserCallback) {
        this.context = null;
        this.privileges = null;
        this.users = users;
        this.removeUserCallback = removeUserCallback;
    }

    public void setOnPrivilegeEditListener(OnPrivilegeEditListener listener) {
        this.editListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.item_privilege_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (privileges != null) {
            bindPrivilegeData(holder, position);
        } else if (users != null) {
            bindUserData(holder, position);
        }
    }
    
    private void bindPrivilegeData(ViewHolder holder, int position) {
        UserRewardPrivilege privilege = privileges.get(position);
        
        // 设置用户信息
        holder.username.setText(privilege.getUsername());
        
        // 设置特权状态
        boolean isValid = privilege.isValid();
        holder.status.setText(isValid ? "有效" : "已过期");
        holder.status.setBackgroundResource(isValid ? 
                R.drawable.bg_status_active : R.drawable.bg_status_expired);
        
        // 设置备注
        holder.remark.setText(privilege.getRemark());
        
        // 设置有效期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String validPeriod;
        if (privilege.getEndDate() == null) {
            validPeriod = "永久有效";
        } else {
            validPeriod = dateFormat.format(privilege.getStartDate()) + " 至 " + 
                    dateFormat.format(privilege.getEndDate());
        }
        
        // 以下代码可能会导致编译错误，暂时注释掉
        /* 
        // 设置分成比例
        holder.taskRewardsLayout.setVisibility(View.VISIBLE);
        holder.level1Ratio.setText(String.format(Locale.getDefault(), "%.1f%%", privilege.getLevel1Ratio()));
        holder.level2Ratio.setText(String.format(Locale.getDefault(), "%.1f%%", privilege.getLevel2Ratio()));
        holder.level3Ratio.setText(String.format(Locale.getDefault(), "%.1f%%", privilege.getLevel3Ratio()));
        
        // 设置邀请奖励
        holder.inviteRewardsLayout.setVisibility(View.VISIBLE);
        holder.inviteReward.setText(String.format(Locale.getDefault(), "¥%.2f", privilege.getInviteReward()));
        */
        
        // 设置编辑和删除按钮点击事件
        holder.editButton.setOnClickListener(v -> {
            if (editListener != null) {
                editListener.onEdit(privilege);
            }
        });
        
        holder.deleteButton.setOnClickListener(v -> {
            if (editListener != null) {
                editListener.onDelete(privilege.getId());
            }
        });
    }
    
    private void bindUserData(ViewHolder holder, int position) {
        UserInfo user = users.get(position);
        
        // 设置用户信息
        holder.username.setText(user.getUsername());
        holder.remark.setText(user.getNickname());
        
        // 其他设置可根据需要添加
        
        // 设置删除按钮点击事件
        holder.deleteButton.setOnClickListener(v -> {
            if (removeUserCallback != null) {
                removeUserCallback.accept(user);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (privileges != null) {
            return privileges.size();
        } else if (users != null) {
            return users.size();
        }
        return 0;
    }
    
    public void updateData(List<UserRewardPrivilege> newPrivileges) {
        this.privileges = newPrivileges;
        notifyDataSetChanged();
    }
    
    public void removePrivilege(int id) {
        if (privileges != null) {
            for (int i = 0; i < privileges.size(); i++) {
                if (privileges.get(i).getId() == id) {
                    privileges.remove(i);
                    notifyItemRemoved(i);
                    break;
                }
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView username;
        TextView status;
        TextView remark;
        // 可能导致编译错误的字段
        /*
        View taskRewardsLayout;
        TextView level1Ratio;
        TextView level2Ratio;
        TextView level3Ratio;
        View inviteRewardsLayout;
        TextView inviteReward;
        */
        Button editButton;
        Button deleteButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.tv_username);
            status = itemView.findViewById(R.id.tv_status);
            remark = itemView.findViewById(R.id.tv_remark);
            
            // 可能导致编译错误的初始化
            /*
            taskRewardsLayout = itemView.findViewById(R.id.layout_task_rewards);
            level1Ratio = itemView.findViewById(R.id.tv_level1_ratio);
            level2Ratio = itemView.findViewById(R.id.tv_level2_ratio);
            level3Ratio = itemView.findViewById(R.id.tv_level3_ratio);
            inviteRewardsLayout = itemView.findViewById(R.id.layout_invite_rewards);
            inviteReward = itemView.findViewById(R.id.tv_invite_reward);
            */
            
            editButton = itemView.findViewById(R.id.btn_edit);
            deleteButton = itemView.findViewById(R.id.btn_delete);
        }
    }
    
    public interface OnPrivilegeEditListener {
        void onEdit(UserRewardPrivilege privilege);
        void onDelete(int id);
    }

    /**
     * 设置任务奖励是否启用
     * @param enabled 是否启用
     */
    public void setTaskRewardEnabled(boolean enabled) {
        this.taskRewardEnabled = enabled;
        notifyDataSetChanged();
    }

    /**
     * 设置邀请奖励是否启用
     * @param enabled 是否启用
     */
    public void setInviteRewardEnabled(boolean enabled) {
        this.inviteRewardEnabled = enabled;
        notifyDataSetChanged();
    }

    /**
     * 获取任务奖励是否启用
     * @return 是否启用
     */
    public boolean isTaskRewardEnabled() {
        return taskRewardEnabled;
    }

    /**
     * 获取邀请奖励是否启用
     * @return 是否启用
     */
    public boolean isInviteRewardEnabled() {
        return inviteRewardEnabled;
    }
} 